package cn.edu.djtu.fw.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.djtu.fw.dao.AdminMapper;
import cn.edu.djtu.fw.model.Admin;
import cn.edu.djtu.fw.service.AdminServiceI;
import cn.edu.djtu.fw.util.Encrypt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("adminService")
public class AdminServiceImpl implements AdminServiceI {
	private AdminMapper adminMapper;

	public AdminMapper getAdminMapper() {
		return adminMapper;
	}

	@Autowired
	public void setAdminMapper(AdminMapper adminMapper) {
		this.adminMapper = adminMapper;
	}

	@Override
	public Admin login(Admin admin) {
		admin.setPwd(Encrypt.e(admin.getPwd()));
		return adminMapper.selectByNameAndPwd(admin);
	}

	@Override
	public boolean delete(int id) {
		try {
			adminMapper.deleteByPrimaryKey(id);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean update(Admin admin) {
		try {
			admin.setPwd(Encrypt.e(admin.getPwd()));
			adminMapper.updateByPrimaryKey(admin);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean insert(Admin admin) {
		try {
			admin.setPwd(Encrypt.e(admin.getPwd()));
			adminMapper.insert(admin);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public Admin getAdminById(int id) {
		return adminMapper.selectByPrimaryKey(id);
	}

	@Override
	public Map<String, Object> getAllAdmin(int page, int rows, Admin admin) {
		try {
			PageHelper.startPage(page, rows);
			List<Admin> admins = adminMapper.getAllAdmin(admin);
			Map<String, Object> adminMap = new HashMap<String, Object>();
			adminMap.put("rows", admins);
			adminMap.put("total", ((Page<Admin>) admins).getTotal());
			return adminMap;
		} catch (Exception e) {
		}
		return null;
	}
}
